<?
require_once("lang.php");

$start_time = microtime_float();

//ob_start("callback");

//Đếm số lượng người truy cập
$db_visited	= new db_execute("UPDATE visited SET vis_counter = vis_counter + 1");
unset($db_visited);

$iCat = getValue('iCat');
$db_cat = new db_query('SELECT cat_id,cat_name,cat_all_child,cat_has_child FROM categories_multi WHERE cat_type = "product" AND cat_id = ' . $iCat . ' AND lang_id = ' . $lang_id);
$current_cat = mysql_fetch_array($db_cat->result);
unset($db_cat);

if (!$current_cat)
{
	redirect($lang_path . 'notfound.html');
}

$show = getValue('show', 'int', 'GET', 1); // 0:list, 1:grid

$keyword 	= getValue('keyword', 'str', 'GET', '');
$price 		= getValue('price');
$price_to 	= getValue('price_to');

$sqlWhere = "";

$sqlWhere .= " AND cat_id IN (" . $current_cat['cat_all_child'] . ")";

if ($keyword != "")
{
	$sqlWhere	.= " AND (pro_name LIKE '%" . $keyword . "%' OR pro_teaser LIKE '%" . $keyword . "%' OR pro_description LIKE '%" . $keyword . "%') ";
}

if ($price > 0)
{
	$sqlWhere .= " AND pro_price >= " . $price . " ";
}
if ($price_to < 0)
{
	$sqlWhere .= " AND pro_price <= " . $price_to . " ";
}

//Sort data
$sort			= getValue("sort");
switch($sort){
	case 0: $sqlOrderBy = "pro_date DESC"; break;
	case 1: $sqlOrderBy = "pro_name ASC"; break;
	case 2: $sqlOrderBy = "pro_name DESC"; break;
	case 3: $sqlOrderBy = "pro_price ASC"; break;
	case 4: $sqlOrderBy = "pro_price DESC"; break;
	case 5: $sqlOrderBy = "pro_date ASC,pro_new ASC"; break;
	case 6: $sqlOrderBy = "pro_new DESC,pro_date DESC"; break;
	case 7: $sqlOrderBy = "ph_hits ASC"; break;
	case 8: $sqlOrderBy = "ph_hits DESC"; break;
	default:$sqlOrderBy = "pro_last_updated DESC"; break;
}

//Get page break params
$page_size		= $con_quantity_product_page;
$page_prefix	= tdt('Trang') . ": ";
$normal_class	= "page";
$selected_class= "page_current";
$previous		= "<";
$next			= ">";
$first			= "<<";
$last			= ">>";
$break_type		= 1;//"1 => << < 1 2 [3] 4 5 > >>", "2 => < 1 2 [3] 4 5 >", "3 => 1 2 [3] 4 5", "4 => < >"
$url			= getURL(0,1,1,1,"page");
$db_count		= new db_query("SELECT COUNT(*) AS count
										 FROM (categories_multi, products_multi)
									 	 WHERE cat_id = pro_category_id AND categories_multi.lang_id = " . $lang_id . $sqlWhere);
$listing_count	= mysql_fetch_array($db_count->result);
$total_record	= $listing_count["count"];
$current_page	= getValue("page", "int", "GET", 1);
if($total_record % $page_size == 0) $num_of_page = $total_record / $page_size;
else $num_of_page = (int)($total_record / $page_size) + 1;
if($current_page > $num_of_page) $current_page = $num_of_page;
if($current_page < 1) $current_page = 1;
$db_count->close();
unset($db_count);
//End get page break params
$db_listing	= new db_query("SELECT *
									 FROM (categories_multi, products_multi) INNER JOIN product_hits ON pro_id = ph_id
									 WHERE cat_id = pro_category_id AND categories_multi.lang_id = " . $lang_id . $sqlWhere . "
									 ORDER BY " . $sqlOrderBy . "
									 LIMIT " . ($current_page - 1) * $page_size . ", " . $page_size);

$site_title = $current_cat['cat_name'] . ' | ' . $con_site_title;
$meta_keywords = implode(',', explode(' ', $current_cat['cat_name']));
$meta_description = $site_title . '. ' . $con_meta_description;
if ($current_page > 1)
{
	$site_title .= ' | ' . tdt('Trang') . ' ' . $current_page;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="keywords" content="<?=str_replace("\n", "", htmlspecialchars($meta_keywords))?>">
<meta name="description" content="<?=str_replace("\n", "", htmlspecialchars($meta_description))?>">
<link href="<?=$gs_imagepath?>favicon.ico" rel="icon" type="image/x-icon" />
<link href="<?=$gs_imagepath?>favicon.ico" rel="shortcut icon" />
<title><?=$site_title?></title>
<style type="text/css" media="all">@import "<?=$theme_csspath?>main.css";</style>
<style type="text/css" media="all">@import "<?=$theme_csspath?>thickbox.css";</style>
<style type="text/css" media="all">@import "<?=$theme_imagepath?>theme.css";</style>
</head>
<body<?=$body_style?>>
<div id="body"<?=$div_id_body_style?>>
	<div id="container_body">
    	<div id="container_header"><? include($real_path . "includes/inc_header.php");?></div>
        <div id="container_content">
        	<div id="container_content_left">
			<?
			include($real_path . "includes/inc_module_left_menu_product.php");
			include($real_path . "includes/inc_module_online_support.php");
			include($real_path . "includes/inc_module_left_banner.php");
			include($real_path . "includes/inc_module_statistic.php");
			?>
            </div>
            <div id="container_content_center">
            <?
			include($real_path . "includes/inc_module_center_search_form.php");
			include($real_path . "includes/inc_type_product.php");
			?>
            </div>
            <div id="container_content_right">
            <?
			include($real_path . "includes/inc_module_hot_news.php");
			//include($real_path . "includes/inc_module_left_menu_news.php");
			include($real_path . "includes/inc_module_right_banner.php");
			?>
            </div>
        	<div class="clear"></div>
        </div>
    	<div id="container_footer"><? include($real_path . "includes/inc_footer.php");?></div>
    </div>
</div>
</body>
</html>
<?
//ob_end_flush();
?>